/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hzf.booklibrary.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import com.hzf.booklibrary.R;
import com.hzf.booklibrary.base.BaseActivity;
import com.hzf.booklibrary.bean.BookClassify;
import com.hzf.booklibrary.component.AppComponent;
import com.hzf.booklibrary.component.DaggerMainComponent;
import com.hzf.booklibrary.ui.contract.BookClassifyContract;
import com.hzf.booklibrary.ui.presenter.BookClassifyPresenter;
import com.hzf.booklibrary.utils.ScreenUtils;
import com.hzf.booklibrary.utils.ToastUtils;
import com.hzf.booklibrary.view.FlowTagLayout;

;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 发现
 *
 * @author yuyh.
 * @date 16/9/1.
 */



public class ClassifyActivity extends BaseActivity implements BookClassifyContract.View{

    class MyTextView extends TextView{

        public MyTextView(Context context) {
            super(context);
        }

        public MyTextView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }

        public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        int id;
        public int getId(){
            return id;
        }

        public void setId( int id ){
            this.id = id;
        }
    }

    @Bind(R.id.ftl_classify)
    FlowTagLayout mFlowTag;
    @Inject
    BookClassifyPresenter mPresenter;

    @Override
    public void initDatas() {
        mPresenter.getBookClassify();//发出请求获取图书分类列表
        showDialog();
    }
    @Override
    public int getLayoutId() {
        return R.layout.activity_classify;
    }
    @Override
    public void configViews() {
        hideStatusBar();
        mPresenter.attachView(this);
    }



    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMainComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void initTitleBar() {
        mBtnLeft.setText("返回");
        mBtnLeft.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.img_back_left),null,null,null);
        mBtnRight.setText("搜索图书");
        mBtnRight.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.img_search),null,null,null);
        tvCenter.setBackgroundResource(R.drawable.img_all_classify);
    }
    @OnClick(R.id.btn_left)
    public void onClickLeft(){
        finish();
    }
    @OnClick(R.id.btn_right)
    public void onClickRight(){
        startActivity(new Intent(this,SearchActivity.class));
    }
    @Override
    public void showBookClassify(List<BookClassify.Classify> data) {
        for (int i=0;i<data.size();i++){
            MyTextView itemView = makeTextView();
            itemView.setText(data.get(i).name);
            itemView.setId(data.get(i).id);
            mFlowTag.addView(itemView);
        }
        dismissDialog();
    }

    @Override
    public void showError() {
        dismissDialog();
        ToastUtils.showSingleToast("加载异常,请检查网络");
    }

    @Override
    public void complete() {}

    private MyTextView makeTextView(){
        final MyTextView textView = new MyTextView(this);
        textView.setBackgroundResource(R.drawable.classify_item_bg);
        textView.setTextColor(getResources().getColorStateList(R.color.selector_classify_item_bg));
        textView.setTextSize(getResources().getDimension(R.dimen.dimen_12dp)/ ScreenUtils.getScreenDensity());

        textView.setPadding(getResources().getDimensionPixelOffset(R.dimen.dimen_10dp)
                ,getResources().getDimensionPixelOffset(R.dimen.dimen_5dp),
                getResources().getDimensionPixelOffset(R.dimen.dimen_10dp)
                ,getResources().getDimensionPixelOffset(R.dimen.dimen_5dp));
        textView.setClickable(true);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tag = textView.getText().toString();
                int id = textView.getId();
                BookListActivity.startActivity(ClassifyActivity.this,tag,id);
            }
        });
        return textView;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter!=null){
            mPresenter.detachView();
        }
    }
}
